package com.taobao.top.api;

import java.util.TreeMap; 
import com.taobao.top.api.exception.ApiException;
import com.taobao.top.beans.AppConstants;
import com.taobao.top.utils.APIUtil;

/**
 * 分销API
 * <p>提供了分销商信息和采购单信息的查询以及分销产品的添加和更新等功能
 * @see http://open.taobao.com/doc/api_cat_detail.htm?cat_id=15&category_id=102
 * @author Fan Gnag http://weibo.com/u/1985223925
 *
 */
public class FenXiao_API {
	
	/**
	 * 获取供应商的合作关系信息
	 * @param format 可选
	 * @param status 可选
	 * @param start_date 可选
	 * @param end_date 可选
	 * @param trade_type 可选
	 * @param page_no 可选
	 * @param page_size 可选
	 * @return 
	 * @throws ApiException
	 */
	public  String getCooperation(String format,String status,String start_date,String end_date,
			String trade_type,String page_no,String page_size) throws ApiException{
		
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		getSystemParam(apiparamsMap);
		
		/* 用户需要自定义返回的格式，默认为XML */
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.cooperation.get",apiparamsMap);
		
		
		/*其他可选参数*/
		putTreeMapParam("status",status, apiparamsMap);
		putTreeMapParam("start_date", start_date,apiparamsMap);
		putTreeMapParam("end_date", end_date, apiparamsMap);
		putTreeMapParam("trade_type", trade_type, apiparamsMap);
		putTreeMapParam("page_no", page_no, apiparamsMap);
		putTreeMapParam("page_size", page_size, apiparamsMap);
		
		//生成签名
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;		
		
	}
	
	/**
	 * 供应商更新合作的分销商等级
	 * @param distributor_id
	 * @param grade_id
	 * @param trade_type 可选
	 * @return
	 * @throws ApiException
	 */
	public String updateCooperation(String distributor_id,String grade_id,String trade_type) throws ApiException{
		TreeMap<String,String>apiparamsMap=new TreeMap<String, String>();
		getSystemParam(apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.cooperation.update",apiparamsMap);
		
		apiparamsMap.put("distributor_id", distributor_id);
		apiparamsMap.put("grade_id", grade_id);
		
		putTreeMapParam(trade_type, trade_type, apiparamsMap);
		
		//生成签名
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;		
				
	}
	
	/**
	 * 新增等级折扣
	 * @param discount_name 必选
	 * @param target_types 必选
	 * @param traget_ids 必选
	 * @param discount_types 必选
	 * @param discount_values 必选
	 * @return
	 * @throws ApiException
	 */
	public String addDiscount(String discount_name,String target_types,
			String traget_ids,String discount_types,String discount_values)throws ApiException {
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.discount.add",apiparamsMap);
		
		apiparamsMap.put("discount_name", discount_name);
		apiparamsMap.put("target_types", target_types);
		apiparamsMap.put("traget_ids", traget_ids);
		apiparamsMap.put("discount_types", discount_types);
		apiparamsMap.put("discount_values", discount_values);
		
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	/**
	 * 修改等级折扣
	 * @param discount_id
	 * @param discount_name
	 * @param discount_status
	 * @param detail_ids
	 * @param target_types
	 * @param target_ids
	 * @param discount_types
	 * @param discount_values
	 * @param detail_statuss
	 * @return
	 * @throws ApiException
	 */
	public String updateDiscount(String discount_id,String discount_name,String discount_status,
			String detail_ids,String target_types,String target_ids,String discount_types,
			String discount_values,String detail_statuss,String format) throws ApiException {
		
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		/* 用户需要自定义返回的格式，默认为XML */
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.discount.update",apiparamsMap);
		
		putTreeMapParam("discount_id", discount_id, apiparamsMap);
		putTreeMapParam("discount_name", discount_name, apiparamsMap);
		putTreeMapParam("detail_statuss", discount_status, apiparamsMap);
		putTreeMapParam("detail_ids",detail_ids,apiparamsMap);
		putTreeMapParam("target_types", target_types, apiparamsMap);
		putTreeMapParam("target_ids", target_ids, apiparamsMap);
		putTreeMapParam("discount_types", discount_types, apiparamsMap);
		putTreeMapParam("discount_values", discount_values, apiparamsMap);
		putTreeMapParam("detail_statuss", detail_statuss, apiparamsMap);
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	/**
	 * 查询折扣信息 
	 * @param discount_id
	 * @param ext_fields
	 * @return
	 * @throws ApiException
	 */
	public String getDiscounts(String discount_id,String ext_fields,String format) throws ApiException {
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		/* 用户需要自定义返回的格式，默认为XML */
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.discounts.get",apiparamsMap);
		
		
		putTreeMapParam("discount_id", discount_id, apiparamsMap);
		putTreeMapParam("ext_fields", ext_fields, apiparamsMap);
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	
	/**
	 * 
	 * @param distributor_id
	 * @param start_modified
	 * @param end_modified
	 * @param page_no
	 * @param page_size
	 * @param product_id
	 * @return
	 */
	public String getItemsDistributor(String distributor_id,String start_modified,
			String end_modified,String page_no,String page_size,String product_id,String format) {
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		/* 用户需要自定义返回的格式，默认为XML */
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.distributor.items.get",apiparamsMap);
		
		putTreeMapParam("distributor_id", distributor_id, apiparamsMap);
		putTreeMapParam("start_modified", start_modified, apiparamsMap);
		putTreeMapParam("end_modified", end_modified, apiparamsMap);
		putTreeMapParam("page_no", page_no, apiparamsMap);
		putTreeMapParam("page_size", page_size, apiparamsMap);
		putTreeMapParam("product_id", product_id, apiparamsMap);
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	public String  getDistributors(String nicks,String format) throws ApiException {
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.distributors.get",apiparamsMap);
		
		putTreeMapParam("nicks", nicks, apiparamsMap);
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	/**
	 * 分销商等级查询
	 * @param format
	 * @return
	 */
	public String getGrades(String format) {
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam("method", "",apiparamsMap);
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	/**
	 * 获取分销用户登陆信息
	 * @param format
	 * @return
	 */
	public String getUserLogin(String format) {
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.login.user.get",apiparamsMap);
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	/**
	 * 供应商确认收款（非支付宝交易）。 
	 * @param purchase_order_id
	 * @param confirm_remark
	 * @param format
	 * @return
	 */
	
	public String paidConfirmOrder(String purchase_order_id,String confirm_remark,String format) {
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.order.confirm.paid",apiparamsMap);
		
		apiparamsMap.put("purchase_order_id", purchase_order_id);
		
		putTreeMapParam("confirm_remark", confirm_remark, apiparamsMap);
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	/**
	 * 分销商或供应商均可用此接口查询采购单信息. (发货处理请调用物流API中的发货接口) 
	 * @param status
	 * @param start_created
	 * @param end_created
	 * @param time_type
	 * @param page_no
	 * @param page_size
	 * @param purchase_order_id
	 * @param format
	 * @return
	 */	
	public String getOrders(String status,String start_created,
			String end_created,String time_type,String page_no,
			String page_size,String purchase_order_id,String format) {
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.orders.get",apiparamsMap);
		
		putTreeMapParam("status", status, apiparamsMap);
		putTreeMapParam("start_created", start_created, apiparamsMap);
		putTreeMapParam("end_created", end_created, apiparamsMap);
		putTreeMapParam("time_type", time_type, apiparamsMap);
		putTreeMapParam("page_no", page_no, apiparamsMap);
		putTreeMapParam("page_size", page_size, apiparamsMap);
		putTreeMapParam("purchase_order_id", purchase_order_id, apiparamsMap);
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	/**
	 * 添加分销平台产品数据。业务逻辑与分销系统前台页面一致。 * 产品图片默认为空 * 产品发布后默认为下架状态 
	 * @param name
	 * @param productcat_id
	 * @param standard_price
	 * @param cost_price
	 * @param retail_price_low
	 * @param retail_price_high
	 * @param category_id
	 * @param outer_id
	 * @param quantity
	 * @param alarm_number
	 * @param desc
	 * @param prov
	 * @param city
	 * @param postage_type
	 * @param postage_id
	 * @param postage_ordinary
	 * @param postage_fast
	 * @param postage_ems
	 * @param have_invoice
	 * @param have_guarantee
	 * @param discount_id
	 * @param trade_type
	 * @param is_authz
	 * @param format
	 * @return
	 */
	public String addProduct(String name,String productcat_id,
			String standard_price,String cost_price,String retail_price_low,
			String retail_price_high,String category_id,String outer_id,
			String quantity,String alarm_number,String desc,String prov,
			String city,String postage_type,String postage_id,
			String postage_ordinary,String postage_fast,String postage_ems,
			String have_invoice,String have_guarantee,String discount_id,
			String trade_type,String is_authz,String format) {
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam("method", "",apiparamsMap);
		
		apiparamsMap.put("name", name);
		apiparamsMap.put("productcat_id", productcat_id);
		apiparamsMap.put("standard_price",standard_price);
		apiparamsMap.put("cost_price",cost_price);
		apiparamsMap.put("retail_price_low", retail_price_low);
		apiparamsMap.put("retail_price_high",retail_price_high);
		apiparamsMap.put("category_id",category_id);	
		putTreeMapParam("outer_id", outer_id,apiparamsMap);
		apiparamsMap.put("quantity", quantity);
		apiparamsMap.put("alarm_number",alarm_number);
		apiparamsMap.put("desc", desc);
		apiparamsMap.put("prov",prov);
		apiparamsMap.put("city",city);
		apiparamsMap.put("postage_type",postage_type);
		putTreeMapParam("postage_id", postage_id,apiparamsMap);
		putTreeMapParam("postage_ordinary",postage_ordinary,apiparamsMap);
		putTreeMapParam("postage_fast",postage_fast,apiparamsMap);
		putTreeMapParam("postage_ems",postage_ems,apiparamsMap);
		apiparamsMap.put("have_invoice", have_invoice);
		apiparamsMap.put("have_guarantee", have_guarantee);
		putTreeMapParam("discount_id", discount_id, apiparamsMap);
		putTreeMapParam("trade_type",trade_type, apiparamsMap);
		putTreeMapParam("is_authz", is_authz, apiparamsMap);
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	/**
	 * 
	 * @param pid
	 * @param name
	 * @param standard_price
	 * @param cost_price
	 * @param retail_price_low
	 * @param retail_price_high
	 * @param outer_id
	 * @param quantity
	 * @param alarm_number
	 * @param desc
	 * @param prov
	 * @param city
	 * @param postage_type
	 * @param postage_id
	 * @param postage_ordinary
	 * @param postage_fast
	 * @param postage_ems
	 * @param status
	 * @param sku_ids
	 * @param sku_cost_prices
	 * @param sku_quantitys
	 * @param sku_outer_ids
	 * @param have_invoice
	 * @param have_guarantee
	 * @param discount_id
	 * @param sku_standard_prices
	 * @param sku_properties
	 * @param format
	 * @return
	 */
	public String updateProduct(String pid,String name,String standard_price,
			String cost_price,String retail_price_low,String retail_price_high,
			String outer_id,String quantity,String alarm_number,String desc,
			String prov,String city,String postage_type,String postage_id,
			String postage_ordinary,String postage_fast,String postage_ems,
			String status,String sku_ids,String sku_cost_prices,String sku_quantitys,
			String sku_outer_ids,String have_invoice,String have_guarantee,
			String discount_id,String sku_standard_prices,String sku_properties,String format) {
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.product.update",apiparamsMap);
		
		apiparamsMap.put("pid",pid);
		putTreeMapParam("name", name, apiparamsMap);
		putTreeMapParam("standard_price", standard_price, apiparamsMap);
		putTreeMapParam("cost_price", cost_price, apiparamsMap);
		putTreeMapParam("retail_price_low", retail_price_low, apiparamsMap);
		putTreeMapParam("retail_price_high", retail_price_high, apiparamsMap);
		putTreeMapParam("outer_id", outer_id, apiparamsMap);
		putTreeMapParam("quantity", quantity, apiparamsMap);
		putTreeMapParam("alarm_number",alarm_number,apiparamsMap);
		putTreeMapParam("desc",desc, apiparamsMap);
		putTreeMapParam("prov", prov,apiparamsMap);
		putTreeMapParam("city",city,apiparamsMap);
		putTreeMapParam("postage_id",postage_id,apiparamsMap);
		putTreeMapParam("postage_ordinary", postage_ordinary, apiparamsMap);
		putTreeMapParam("postage_fast",postage_fast,apiparamsMap);
		putTreeMapParam("postage_ems",postage_ems,apiparamsMap);
		putTreeMapParam("postage_type", postage_type,apiparamsMap);
		putTreeMapParam("status",status, apiparamsMap);
		putTreeMapParam("sku_ids",sku_ids,apiparamsMap);
		putTreeMapParam("sku_cost_prices",sku_cost_prices,apiparamsMap);
		putTreeMapParam("sku_quantitys",sku_quantitys,apiparamsMap);
		putTreeMapParam("sku_quantitys",sku_quantitys,apiparamsMap);
		putTreeMapParam("sku_outer_ids",sku_outer_ids, apiparamsMap);
		putTreeMapParam("have_invoice",have_invoice,apiparamsMap);
		putTreeMapParam("have_guarantee",have_guarantee,apiparamsMap);
		putTreeMapParam("discount_id",discount_id,apiparamsMap);
		putTreeMapParam("sku_standard_prices", sku_standard_prices,apiparamsMap);
		putTreeMapParam("sku_properties",sku_properties,apiparamsMap);
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	
	/**
	 * 查询供应商的所有产品线数据。根据登陆用户来查询，不需要其他入参 
	 * @param fields
	 * @param format
	 * @return
	 */
	public String getProductcats(String fields,String format) {
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.productcats.get",apiparamsMap);
		
		putTreeMapParam(fields, fields, apiparamsMap);
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	
	/**
	 * 查询供应商的产品数据。 * 入参传入pids将优先查询，即只按这个条件查询。 *入参传入sku_number将优先查询(没有传入pids)，即只按这个条件查询(最多显示50条) * 入参fields传skus将查询sku的数据，不传该参数默认不查询，返回产品的其它信息。 * 入参fields传入images将查询多图数据，不传只返回主图数据。 * 入参fields仅对传入pids生效（只有按ID查询时，才能查询额外的数据） * 查询结果按照产品发布时间倒序，即时间近的数据在前。
	 * @param outer_id
	 * @param productcat_id
	 * @param status
	 * @param pids
	 * @param fields
	 * @param start_modified
	 * @param end_modified
	 * @param page_no
	 * @param page_size
	 * @param sku_number
	 * @param is_authz
	 * @param format
	 * @return
	 */
	public String getProducts(String outer_id,String productcat_id,String status,
			String pids,String fields,String start_modified,String end_modified,
			String page_no,String page_size,String sku_number,String is_authz,String format) {
		TreeMap<String, String> apiparamsMap=new TreeMap<String, String>();
		
		getSystemParam(apiparamsMap);
		
		putTreeMapParam("format", format, apiparamsMap);
		
		putTreeMapParam(outer_id, outer_id, apiparamsMap);
		putTreeMapParam(productcat_id, productcat_id, apiparamsMap);
		putTreeMapParam(status, status, apiparamsMap);
		putTreeMapParam(pids, pids, apiparamsMap);
		putTreeMapParam(fields, fields, apiparamsMap);
		putTreeMapParam(start_modified, start_modified, apiparamsMap);
		putTreeMapParam(end_modified, end_modified, apiparamsMap);
		putTreeMapParam(page_no, page_no, apiparamsMap);
		putTreeMapParam(page_size, page_size, apiparamsMap);
		putTreeMapParam(sku_number, sku_number, apiparamsMap);
		putTreeMapParam(is_authz, is_authz, apiparamsMap);
		
		putTreeMapParam("method", "taobao.fenxiao.products.get",apiparamsMap);
		
		String sgin=APIUtil.md5Signature(apiparamsMap, AppConstants.SECRET);
		apiparamsMap.put("sgin", sgin);
		
		String paramsString = APIUtil.getContent(apiparamsMap);
		String responseData = APIUtil.getResult(AppConstants.APIURL,
				paramsString);

		return responseData;
	}
	
	private void putTreeMapParam(String name,String value,TreeMap<String,String> sTreeMap) {
		if(value !=null && !value.trim().equals("")){
			sTreeMap.put(name, value);
		}
	}
	
	private void getSystemParam(TreeMap<String, String> sTreeMap){
		sTreeMap.putAll(APIUtil.getSystemParams());
	}
}
